<template>
  <div>
    <div class="text-input"
         @mouseenter="onMouseEnter"
         @mouseleave="onMouseLeave">
      <div v-show="!editMode">
        <div class="text-input-text"
             :class="textAnimClass">{{label}}：{{ date[0] }} {{ date[1] }}</div>
        <i class="el-icon-edit text-input-icon"></i>
      </div>
      <el-date-picker v-show="editMode"
                      v-model="date"
                      class="text-input-input"
                      :class="inputAnimClass"
                      type="daterange"
                      value-format="yyyy-MM-dd"
                      align="right"
                      unlink-panels
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      @change="changeDatePicker"
                      @blur="onInputBlur">
      </el-date-picker>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DatePickerValue',
  model: {
    prop: "date",
    event: "input"
  },
  props: {
    date: {
      type: Array,
      default: () => []
    },
    label: {
      type: String,
      default: ''
    },
    // date: {
    //   type: Array,
    //   default: () => []
    // }
  },
  data () {
    return {
      editMode: false,
      textAnimClass: '',
      inputAnimClass: ''
    }
  },
  created () {
    // this.pickerValue = this.value
  },
  methods: {
    onMouseEnter () {
      this.textAnimClass = 'fadeOut';
      this.inputAnimClass = 'fadeIn';
      this.editMode = true;
    },
    onMouseLeave () {
      this.textAnimClass = 'fadeIn';
      this.inputAnimClass = 'fadeOut';
      this.timer = setTimeout(() => {
        this.editMode = false;
      }, 200);
    },
    onInputBlur () {
      this.editMode = false;
    },
    changeDatePicker (value) {
      this.$emit('input', value)
    },
    beforeDestroy () {
      this.$once("hook:beforeDestroy", () => clearInterval(this.timer));
    },
  }
}
</script>

<style scoped>
.text-input {
  width: 300px;
  height: 32px;
  line-height: 32px;
  position: relative;
}
.text-input-icon {
  /* position: absolute;
  top: 0;
  left: 0;
  width: 95%;
  height: 100%;
  line-height: 32px;
  text-align: right; */
  font-size: 14px;
  transition: all 0.3s;
  color: var(--prev-color-primary);
  opacity: 1;
}
.text-input-text {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  line-height: 32px;
  text-align: center;
  font-size: 14px;
  transition: all 0.3s;
  opacity: 1;
}
.text-input-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  font-size: 14px;
  transition: all 0.3s;
  opacity: 0;
}
.fadeIn {
  opacity: 1;
}
.fadeOut {
  opacity: 0;
}
</style>